package com.neu.questionnaire_system.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neu.questionnaire_system.entity.Answer;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xingle
 * @since 2022-10-31
 */
@Mapper
public interface AnswerMapper extends BaseMapper<Answer> {
    //使用xml来插入答者
    int addAnswerInfo(Map<String,Object> map);

    //查询答者名是否存在
    int queryExistAnswer(Map<String,Object> map);

    //查询所有答者列表
    List<Map<String,Object>> showAnswer();

    //根据答者名删除
    @Delete("delete from answer where answerName=#{answerName,jdbcType=VARCHAR}")
    int deleteAnswer(Map<String,Object> map);

    //用户根据答者名查询答者信息，返回一个包含答者信息的map
    List<Map<String,Object>> findAnswerByAnswerName(Map<String,Object> map);

    //更新答者信息
    int updateAnswer(Map<String,Object> map);

    //登录查询答者信息
    @Select(" SELECT * " +
            "        FROM answer\n" +
            "        WHERE answerName = #{answerName} AND `password` = #{password} AND isDeleted='0'")
    Answer queryAnswerInfo(@Param("answerName") String answerName,@Param("password") String password);

    //通过答者名查询其aid
    String queryAnswerByAnswerName(String answerName);

    //通过aid查询是否有群组
    int queryClusterByAid(String aid);

    //根据答者名判断答者是否存在，如果存在返回大于0
    int queryAnswerIsEmpty(String AnswerName);

    //查询所有答者列表
    IPage<Map<String,Object>> showAnswer(@Param("page") Page page);

    String queryAidByAnswerName(String answerName);

    @Update("update Answer set isDeleted=#{isDeleted,jdbcType=VARCHAR} where aid=#{aid,jdbcType=VARCHAR}")
    int updateAnswerStatue(@Param("isDeleted")String isDeleted,@Param("aid")String aid);

    @Select("select count(2) from answer_cluster where aid =#{aid}")
    int ifAnswerCanDelete(@Param("aid")String aid);

    @Select("select count(q.qid) counts,qh.questionId,q.qid from question_history qh " +
            "left join question q " +
            "on qh.questionId  = q.questionId " +
            "left join answer a " +
            "on a.aid = qh.aid " +
            "where a.answerName = #{answerName} and q.qid = #{qid}")
    List<Map<String,Object>> isAnsweredQuestionnaire(@Param("answerName")String answerName,@Param("qid")String qid);

    @Select("select count(1) from answer_cluster where aid =#{aid} and qid =#{qid}")
    int findAnswerCluster(@Param("aid")String aid,@Param("qid")String qid);

}
